package com.example.cyw.sqlhelper;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.bumptech.glide.Glide;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ComcontActivity extends AppCompatActivity {
    private Button btn_back,btn_save;
    private TextView tv_time;
    private VideoView videoView;
    private EditText ed_comcont;
    private ImageView img;
    private ImageButton btn_paizhao,btn_luxiang,btn_luyin;
    private Uri imageUri,VideoUri;
    private static final int RESULT_CAMERA=200;
    private int REQUEST_CODE_PICK_IMAGE=1001;
   private String picturePath;
    private final static int REQUEST_RECORDER = 100;
    private Uri Radiouri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comcont);
        Log.e("TAG, ","onCreate: "+111 );
        initView();
        btn_paizhao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             showDialog();
            }
        });
        btn_luyin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                luyin();
            }
        });
        btn_luxiang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String status=Environment.getExternalStorageState();
                if(status.equals(Environment.MEDIA_MOUNTED))
                {
                    File outputVideo=new File(getExternalCacheDir(),"out_Video"+Math.random()+".3gp");
                    try {
                        outputVideo.createNewFile();
                        VideoUri=Uri.fromFile(outputVideo);

                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
                Intent intent = new Intent();
                 intent.setAction("android.media.action.VIDEO_CAPTURE");
                intent.addCategory("android.intent.category.DEFAULT");
                intent.putExtra(MediaStore.EXTRA_OUTPUT,VideoUri);
                startActivityForResult(intent, 0);
            }
        });

    }

    private void luyin() {
        try {
            Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
            startActivityForResult(intent, REQUEST_RECORDER);
        }catch (Exception e){
            Toast.makeText(this, "请打开录音的权限", Toast.LENGTH_SHORT).show();
        }
    }

    private void showDialog() {
        final Dialog dialog = new Dialog(this, R.style.NormalDialogStyle);
        View view = View.inflate(this, R.layout.dialog_bottom, null);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(true);
        view.setMinimumHeight((int) (ScreenSizeUtils.getInstance(this).getScreenHeight() * 0.23f));
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (ScreenSizeUtils.getInstance(this).getScreenWidth() * 0.9f);
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        dialogWindow.setAttributes(lp);
        dialog.show();
       Button btn_camer= view.findViewById(R.id.btn_camer);
       btn_camer.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               takePhoto();
               dialog.dismiss();
               ;
           }

       });
       Button btn_xiangce=view.findViewById(R.id.btn_xiangce);
       btn_xiangce.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               takepicture();
               dialog.dismiss();
           }
       });
      Button btn_quxiao=view.findViewById(R.id.btn_quxiao);
      btn_quxiao.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              dialog.dismiss();
          }
      });


    }

    private void takepicture() {
        try {
            Intent intent=new Intent(Intent.ACTION_PICK);
            intent.setType("image/*");
            startActivityForResult(intent,REQUEST_CODE_PICK_IMAGE);
        }catch (Exception e){
            Toast.makeText(this, "请打开相机的权限", Toast.LENGTH_SHORT).show();
        }
    }

    private void takePhoto() {

            String status=Environment.getExternalStorageState();
            if(status.equals(Environment.MEDIA_MOUNTED))
            {
                //创建File对象，用于存储拍照后的照片
                File outputImage=new File(getExternalCacheDir(),"out_image"+Math.random()+".jpg");//SD卡的应用关联缓存目录
                try {

                    outputImage.createNewFile();
                    Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        imageUri=FileProvider.getUriForFile(ComcontActivity.this,
                                "com.hanrui.android.fileprovider",outputImage);//添加这一句表示对目标应用临时授权该Uri所代表的文件
                        Log.e("TAG", "takePhoto: >" );
                    }else{
                        imageUri=Uri.fromFile(outputImage);
                    }
                    //启动相机程序
                    intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
                   intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
                    startActivityForResult(intent,RESULT_CAMERA);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    Toast.makeText(ComcontActivity.this, "没有找到储存目录",Toast.LENGTH_LONG).show();
                }
            }else{
                Toast.makeText(ComcontActivity.this, "没有储存卡",Toast.LENGTH_LONG).show();
            }


    }



    private void initView() {
        btn_paizhao=findViewById(R.id.btn_paizhao);
        img=findViewById(R.id.img);
        btn_luyin=findViewById(R.id.btn_luyin);
        btn_luxiang=findViewById(R.id.btn_luxiang);
        videoView=findViewById(R.id.video);
        

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case RESULT_CAMERA:
                if (resultCode == RESULT_OK) {
                    //进行裁剪
                    Log.e("TAG", "onActivityResult: "+imageUri.toString() );

                    Glide.with(this).load(imageUri).into(img);
                }
                break;
            case 1001:
                if (resultCode == RESULT_OK) {
                    try {
                        Uri uri = data.getData();
                        String[] filePathColumn = {MediaStore.Images.Media.DATA};
                        Cursor cursor = ComcontActivity.this.getContentResolver().query(uri,
                                filePathColumn, null, null, null);
                        cursor.moveToFirst();
                        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                        picturePath = cursor.getString(columnIndex);
                        Log.e("reg", "filePathColumn" + filePathColumn[0]);
                        Log.e("reg", "index" + columnIndex);
                        Log.e("reg", "picturePath" + picturePath);
                        Glide.with(ComcontActivity.this).load(picturePath).into(img);
                        cursor.close();

                    } catch (Exception e) {
                        Toast.makeText(this, "请打开读写手机内存权限", Toast.LENGTH_SHORT).show();
                    }
                    break;

                }
                case 0:
                    if (resultCode == RESULT_OK){

                    }
                    break;
        }
    }
}
